﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using myBoard.Framework.Package;
using System.IO;

namespace myBoard.Framework.Test.Package
{
    /// <summary>
    /// Summary description for PackageManager_Test
    /// </summary>
    [TestClass]
    public class PackageManager_Test
    {
        public PackageManager_Test()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        //[TestMethod]
        public void PackFileInStream()
        {
            Stream stream = PackageManager.PackFileInStream(@"C:\temp\TestFolder\TestFile1.txt");
            Assert.IsNotNull(stream);
        }

        //[TestMethod]
        public void PackFileOnDisk()
        {
            PackFileOnDisk(@"C:\temp\TestFolder\TestFile1.txt",
                    @"C:\temp\TestFolder\packedFiles\");
        }

        //[TestMethod]
        public void PackFolderOnDisk()
        {
            PackFolderOnDisk(@"C:\temp\TestFolder\Folder1",
                    @"C:\temp\TestFolder\packedFolders\Folder1.mgt");
        }

        //[TestMethod]
        public void UnpackFile()
        {
            string inputFile = @"C:\temp\TestFolder\TestFile1.txt"; 
            string packedFileOutputFolder = @"C:\temp\TestFolder\packedFiles\";
            string unpackedFileOutputFolder = @"C:\temp\TestFolder\output\";            
            PackFileOnDisk(inputFile, packedFileOutputFolder);
            string packedFile = Path.Combine(packedFileOutputFolder,Path.GetFileNameWithoutExtension(inputFile)+".mgt");
            UnpackFile(packedFile, unpackedFileOutputFolder);
            Assert.IsTrue(File.Exists(Path.Combine(unpackedFileOutputFolder, Path.GetFileName(inputFile))));
        }
        //[TestMethod]
        public void UnpackFile2()
        {
            //string inputFile = @"C:\temp\TestFolder\TestFile1.txt";
            //string packedFileOutputFolder = @"C:\temp\TestFolder\packedFiles\";
            string unpackedFileOutputFolder = @"C:\temp\TestFolder\output\";
            //PackFileOnDisk(inputFile, packedFileOutputFolder);
            //string packedFile = Path.Combine(packedFileOutputFolder, Path.GetFileNameWithoutExtension(inputFile) + ".mgt");
            UnpackFile(@"C:\temp\TestFolder\packedFolders\Folder1.mgt", unpackedFileOutputFolder);
            //Assert.IsTrue(File.Exists(Path.Combine(unpackedFileOutputFolder, Path.GetFileName(inputFile))));
        }

        private void PackFolderOnDisk(string inputFolder, string outputPackedFolder)
        {
            string outputFile = Path.Combine(outputPackedFolder, Path.GetFileName(inputFolder));
            if (Directory.Exists(outputFile))
                Directory.Delete(outputFile,true);
            PackageManager.PackFolderOnDisk(inputFolder, outputPackedFolder);
            Assert.IsTrue(File.Exists(Path.Combine(outputPackedFolder, Path.GetFileName(inputFolder))));
        }

        private void PackFileOnDisk(string inputFile,string outputFolder)
        {
            string outputFile = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputFile)+".mgt");
            if (File.Exists(outputFile))
                File.Delete(outputFile);
            PackageManager.PackFileOnDisk(inputFile,outputFolder);
            Assert.IsTrue(File.Exists(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputFile) + ".mgt")));            
        }

        private void UnpackFile(string inputFile, string outputFolder)
        {
            PackageManager.UnpackFile(inputFile, outputFolder);
        }       
    }
}
